package cn.cdyx.common.controller;

import cn.cdyx.common.exception.BusinessException;
import cn.cdyx.common.exception.SystemException;
import cn.cdyx.common.pojo.Code;
import cn.cdyx.common.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.time.LocalDateTime;

/**
 * @Author Watermelon
 * @Date 2023-01-16 11:09
 * 控制器错误返回类
 */
@RestControllerAdvice
@Slf4j
public class SysExceptionAdvice {
    @ExceptionHandler(SystemException.class)
    public Result doSystemException(SystemException ex){
        log.error("系统错误-日志：{}", new Result(ex.getCode(),ex.getMessage(), LocalDateTime.now(),null));
        return new Result(ex.getCode(),ex.getMessage(), LocalDateTime.now(),null);
    }
    @ExceptionHandler(BusinessException.class)
    public Result doBusinessException(BusinessException ex){
        log.error("业务错误-日志：{}", new Result(ex.getCode(),ex.getMessage(), LocalDateTime.now(),null));
        return new Result(ex.getCode(),ex.getMessage(),LocalDateTime.now(),null);
    }

    @ExceptionHandler(Exception.class)
    public Result doOtherException(Exception ex){
        log.error("未知错误-日志：{}", new Result(Code.SYSTEM_UNKNOW_ERR,ex.getMessage(), LocalDateTime.now(),null));
        return new Result(Code.SYSTEM_UNKNOW_ERR,"接口内部异常！请联系HIS查看！",LocalDateTime.now(),null);
    }
}
